from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from goods.models import *
from .serializers import *
from rest_framework.pagination import PageNumberPagination

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from .filters import GoodsFilter
from rest_framework.throttling import UserRateThrottle, AnonRateThrottle


# 分页
class FenYe(PageNumberPagination):

    page_size = 12
    page_query_param = 'page'


# 商品数据视图类
class GoodsListView(ModelViewSet):

    queryset = Goods.objects.all()
    serializer_class = GoodSerializer
    pagination_class = FenYe
    # 过滤
    filter_backends = (DjangoFilterBackend,
                       filters.SearchFilter,
                       filters.OrderingFilter)
    filter_class = GoodsFilter
    # 搜索
    search_fields = ('name', 'goods_brief', 'goods_desc')
    # 排序
    ordering_fiel  d    s = ('shop_price', 'sold_num')
    # 限速
    throttle_classes = (UserRateT hrottle, AnonRateThrottle)


# 商品分类列表数据
class CategoryViewset(ModelViewSet):

    queryset = GoodsCategory.objects.filter(category_type=1)
    serializer_class = GoodsCategorySerializer


class HotSearchViewset(ModelViewSet):
    queryset = HotSearch.objects.all().order_by('-index')[:5]
    serializer_class = HotSearchSeriailzer


# 轮播图
class BannerViewset(ModelViewSet):

    queryset = Banner.objects.all().order_by('index')
    serializer_class = BannerSeriailzer


# 首页商品分类展示
class IndexGoodsViewset(ModelViewSet):

    # 只需显示导航栏的一级分类
    queryset = GoodsCategory.objects.filter(is_tab=True)
    serializer_class = IndexCategorySerializer




